<?php 
require '../../config.php';
$idcontrato=$_REQUEST['idcontrato'];
$idcliente=$_REQUEST['idcliente'];

if (isset($_REQUEST['regreso']))$regreso=$_REQUEST['regreso'];else $regreso=0;
$query="SELECT pagos.*,us.nombre,us.apellido,cli.nombre as cliente
FROM pagosclientesventas pagos, usuarios us, clientesventas cli,contratos con
where pagos.idcontrato = ".$idcontrato."
and us.idusuarios = pagos.usuario
and pagos.idcontrato = con.idcontratos
and con.cliente = cli.idclientesventas";
$querysum ="SELECT sum(monto) as suma
FROM pagosclientesventas 
where idcontrato = ".$idcontrato;


$resultset = mysql_query($query,$conexion);

if(!$resultset)
{
	mysql_close();
	echo ('error en el query: ' . mysql_error());
}
elseif(!mysql_num_rows($resultset))
{
	mysql_close();
	echo "<legend>Sin resultados</legend>";
	if($regreso == 0)
		echo "<button type='button' class='btn btn-link btn-xs'  onclick='ver2($idcliente);'><span class='glyphicon glyphicon-arrow-left'></span><font size='3'> Regresar</font></button>";
	else if ($regreso == 1)
		echo "<button type='button' class='btn btn-link btn-xs'  onclick='verContrato($idcontrato)'><span class='glyphicon glyphicon-arrow-left'></span><font size='3'> Regresar</font></button>";
	else if ($regreso == 2)
		echo "<button type='button' class='btn btn-link btn-xs'  onclick='verContrato2($idcontrato)'><span class='glyphicon glyphicon-arrow-left'></span><font size='3'> Regresar</font></button>";
	
}
else
{
	echo "<legend>Historial de Pago</legend>";
	if($regreso == 0)
		echo "<button type='button' class='btn btn-link btn-xs'  onclick='ver2($idcliente);'><span class='glyphicon glyphicon-arrow-left'></span><font size='3'> Regresar</font></button>";
	else if ($regreso == 1)
		echo "<button type='button' class='btn btn-link btn-xs'  onclick='verContrato($idcontrato)'><span class='glyphicon glyphicon-arrow-left'></span><font size='3'> Regresar</font></button>";
	else if ($regreso == 2)
		echo "<button type='button' class='btn btn-link btn-xs'  onclick='verContrato2($idcontrato)'><span class='glyphicon glyphicon-arrow-left'></span><font size='3'> Regresar</font></button>";
	
	echo   "<table class='table table-bordered' style='background-color: #f2f9f2'>";
					
						echo   "<tr class='success'>";
						echo   "<th>Contrato</th><th>Cliente</th><th>Fecha</th><th>Monto</th><th>Observaci&oacute;n</th><th>Usuario</th>";
						echo   "</tr>";
						while($row = mysql_fetch_assoc($resultset))
						{
							echo   "<tr>";
							echo   "<td>".$row['idcontrato']."</td>";
							echo   "<td>".$row['cliente']."</td>";
							echo   "<td>".date('d-m-Y',strtotime($row['fechapago']))."</td>";
							echo   "<td>$".$row['monto']."</td>";
							echo   "<td>".$row['observacion']."</td>";
							echo   "<td>".$row['nombre']." ".$row['apellido']."</td>";
							
							echo   "</tr>";
						}
						$resultset2 = mysql_query($querysum,$conexion);
						$row2 = mysql_fetch_assoc($resultset2);
						echo   "<tr>";
						echo   "<td colspan='6'>Total de Pagos : $ $row2[suma]</td>";
						echo   "</tr>";
						echo   "<tbody>";
						echo   "</tbody>";
						echo   "</table>";
						mysql_close();
}
?>